package Thread;
class Test{
    public int count = 0;

    public void add() {
        //针对this加锁
        synchronized (this) {
            count++;
        }
    }
    //等价于
//    synchronized public void add(){
//        count++;
//    }
    public void func(){
        //给类对象加锁
        synchronized (Test.class){

        }
    }
    //等价于
//    synchronized static void func(){
//
//    }
//    public void  add2(){
//        count++;
//    }
}
public class ThreadDemo20 {
    public static void main(String[] args) throws InterruptedException {
        Test test = new Test();
        Thread thread = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                test.add();
            }
        });
        Thread thread2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                test.add();
            }
        });
        thread.start();
        thread2.start();

        thread.join();
        thread2.join();
        System.out.println("count = "+ test.count);
    }
}
